<template>
  <div class="app-container">
    <el-form :model="searchForm" :inline="true">
      <el-row>
        <el-col :span="20">
          <el-form-item>
            <treeselect
              style="width:180px"
              v-model="positionTwo"
              :options="locationArray"
              :normalizer="normalizerTwo"
              placeholder="监测位置"/>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" class="iconfont icon-chaxun1" @click="handleSearch"> 查询</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-table
      border
      :data="surveyData"
      stripe
      highlight-current-row
      :header-cell-style="{background:'#ECF3FC'}"
    >
      <el-table-column type="index" width="50" label="序号" align="center" />
      <el-table-column prop="monitorLocationName" label="监测位置" width="140px" align="center" />
      <el-table-column prop="sbbh" label="设备编号" width="170px" align="center">
        <div slot-scope="{row}">
          <div v-for="element in row.ambientDeviceVos">
            <div style="border:1px solid #e6ebf5;margin: -1px -11px -1px -11px;padding-top: 5px;padding-bottom: 2px;">
              <el-button type="text" style="color: #606266;cursor: default;">{{ element.deviceCode }}</el-button>
            </div>
          </div>
        </div>
      </el-table-column>
      <el-table-column prop="sbmc" label="设备名称" width="140px" align="center">
        <div slot-scope="{row}">
          <div v-for="element in row.ambientDeviceVos">
            <div style="border:1px solid #e6ebf5;margin: -1px -11px -1px -11px;padding-top: 5px;padding-bottom: 2px;">
              <el-button type="text" style="color: #606266;cursor: default;">{{ element.deviceName }}</el-button>
            </div>
          </div>
        </div>
      </el-table-column>
      <el-table-column prop="sblb" label="设备类别" align="center">
        <div slot-scope="{row}">
          <div v-for="element in row.ambientDeviceVos">
            <div style="border:1px solid #e6ebf5;margin: -1px -11px -1px -11px;padding-top: 5px;padding-bottom: 2px;">
              <span v-if="element.deviceClass === '1'"><el-button type="text" style="color: #606266;cursor: default;">温湿度计</el-button></span>
            </div>
          </div>
        </div>
      </el-table-column>
      <el-table-column prop="sbzt" label="设备状态" align="center">
        <div slot-scope="{row}">
          <div v-for="element in row.ambientDeviceVos">
            <div style="border:1px solid #e6ebf5;margin: -1px -11px -1px -11px;padding-top: 5px;padding-bottom: 2px;">
              <span v-if="element.deviceStatus === '0'"><el-button type="text" style="color: #fff;cursor: default;background-color: rgb(19, 206, 102);padding-left: 10px;padding-right: 10px;">在线</el-button></span>
              <span v-if="element.deviceStatus === '2'"><el-button type="text" style="color: #fff;cursor: default;background-color: rgba(73,69,64,0.65);padding-left: 10px;padding-right: 10px;">停用</el-button></span>
            </div>
          </div>
        </div>
      </el-table-column>
      <el-table-column prop="dqwd" label="当前温度(°C)" align="center">
        <div slot-scope="{row}">
          <div v-for="element in row.ambientDeviceVos">
            <div style="border:1px solid #e6ebf5;margin: -1px -11px -1px -11px;padding-top: 5px;padding-bottom: 2px;">
              <el-button type="text" v-if="element.temperature === null" style="color: #606266;cursor: default;">&nbsp;</el-button>
              <el-button type="text" v-if="element.temperature != null" style="color: #606266;cursor: default;">{{ element.temperature }}</el-button>
            </div>
          </div>
        </div>
      </el-table-column>
      <el-table-column prop="dqsd" label="当前湿度(%rh)" align="center">
        <div slot-scope="{row}">
          <div v-for="element in row.ambientDeviceVos">
            <div style="border:1px solid #e6ebf5;margin: -1px -11px -1px -11px;padding-top: 5px;padding-bottom: 2px;">
              <el-button type="text" v-if="element.humidity === null" style="color: #606266;cursor: default;">&nbsp;</el-button>
              <el-button type="text" v-if="element.humidity != null" style="color: #606266;cursor: default;">{{ element.humidity }}</el-button>
            </div>
          </div>
        </div>
      </el-table-column>
      <el-table-column label="操作" fixed="right" align="center" width="230px">
        <div slot-scope="{row}">
          <div v-for="element in row.ambientDeviceVos">
            <div style="border:1px solid #e6ebf5;margin: -1px -11px -1px -11px;padding-top: 5px;padding-bottom: 2px;">
              <el-button type="text" @click="seeSurvey(element)">查看</el-button>
              <el-button type="text" @click="noteSurvey(element)" v-if="element.deviceStatus === '0'">记录温湿度</el-button>
              <el-button type="text" @click="findAmbientSurveyNote(element)">监测记录</el-button>
            </div>
          </div>
        </div>
      </el-table-column>
    </el-table>
    <div style="float: right">
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="searchForm.pageNumber"
        :limit.sync="searchForm.pageSize"
        @pagination="handleSearch"/>
    </div>

    <!-- 记录温湿度 -->
    <el-dialog title="记录温湿度" :visible.sync="addSurveyVisible" center width="40%" :close-on-click-modal="false">
      <div>
        <el-form ref="surveyForm" :model="surveyForm" :rules="surveyRules" label-width="95px">
          <el-row>
            <el-col :span="11">
              <el-form-item prop="temperature" label="温度:">
                <el-input-number size="small" class="i-input" controls-position="right" v-model="surveyForm.temperature" :step="1" :precision="1" />&nbsp;&nbsp;°C
              </el-form-item>
            </el-col>
            <el-col :span="11">
              <el-form-item prop="humidity" label="湿度:">
                <el-input-number size="small" class="i-input" controls-position="right" v-model="surveyForm.humidity" :step="1" :precision="0" />&nbsp;&nbsp;%rh
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="20">
              <el-form-item prop="surveyDate" label="日期:">
                <el-date-picker v-model="surveyDateCon" type="date" value-format="yyyy-MM-dd" @change="selectDate" placeholder="选择日期"/>
              </el-form-item>
            </el-col>
            <el-col :span="20">
              <el-form-item prop="surveyTime" label="时间:">
                <el-time-picker v-model="surveyTimeCon" value-format="HH:mm:ss" @change="selectTime" placeholder="选择时间"/>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer" style="margin-top: -30px;float: right;">
        <el-button @click="addSurveyVisible=false">取消</el-button>
        <el-button type="primary" @click="confirmSurvey" v-preventReClick>确认</el-button>
      </div>
    </el-dialog>

    <!-- 查看 -->
    <el-dialog title="查看温湿度" :visible.sync="seeSurveyVisible" center width="26%" :close-on-click-modal="false">
      <div>
        <el-form :model="seeSurveyForm" label-width="90px">
          <el-row>
            <el-col :span="11">
              <el-form-item prop="temperature" label="温度:">
                <span style="font-weight: bold;">{{seeSurveyForm.temperature}}</span>&nbsp;°C
              </el-form-item>
            </el-col>
            <el-col :span="11">
              <el-form-item prop="humidity" label="湿度:">
                <span style="font-weight: bold;">{{seeSurveyForm.humidity}}</span>&nbsp;%rh
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="20">
              <el-form-item prop="surveyDate" label="日期:">
                <span style="font-weight: bold;">{{seeSurveyForm.surveyDate}}</span>
              </el-form-item>
            </el-col>
            <el-col :span="20">
              <el-form-item prop="surveyTime" label="时间:">
                <span style="font-weight: bold;">{{seeSurveyForm.surveyTime}}</span>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer" style="margin-top: -30px;float: right;">
        <el-button @click="seeSurveyVisible=false">取消</el-button>
      </div>
    </el-dialog>

    <!-- 监测记录 -->
    <el-dialog title="监测记录查看" :visible.sync="lookSurveyNoteVisible" center width="50%" :close-on-click-modal="false">
      <el-form :model="searchTwoForm" :inline="true">
        <el-row>
          <el-col :span="20">
            <el-form-item>
              <el-date-picker v-model="searchTwoForm.startTime" type="date" value-format="yyyy-MM-dd" placeholder="开始日期" style="width: 35%;"/>&nbsp;至&nbsp;
              <el-date-picker v-model="searchTwoForm.endTime" type="date" value-format="yyyy-MM-dd" placeholder="结束日期" style="width: 35%;"/>&nbsp;&nbsp;
              <el-button type="primary" class="iconfont icon-chaxun1" @click="screenAmbientSurveyNote">筛选</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-table
        border
        :data="surveyTwoData"
        stripe
        highlight-current-row
        :header-cell-style="{background:'#ECF3FC'}">
        <el-table-column type="index" width="80" label="序号" align="center" />
        <el-table-column prop="jcsj" label="监测时间" align="center">
          <template slot-scope="{row}">
            <span>{{row.surveyDate}}&nbsp;{{row.surveyTime}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="temperature" label="当前温度(°C)" align="center" />
        <el-table-column prop="humidity" label="当前湿度(%rh)" align="center" />
      </el-table>
      <div slot="footer" class="dialog-footer" style="margin-top: -15px;" v-if="surveyTwoData.length > 0">
        <el-button @click="lookSurveyNoteVisible=false">取消</el-button>
        <el-button type="primary" icon="el-icon-download" @click="exportsSurvey">下载</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Pagination from '@/components/Pagination'
import * as ambientServer from '@/api/ambient'
import * as instrServer from '@/api/instrument'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'

export default {
  components: { Pagination,Treeselect },
  data() {
    return {
      searchForm: {
        monitorLocation: '',
        pageNumber: 1,
        pageSize: 10
      },
      total: 0,
      surveyData: [],
      locationArray: [],
      positionTwo: null,
      surveyForm: {
        temperature: '',
        humidity: '',
        surveyDate: '',
        surveyTime: '',
        ambientDeviceId: ''
      },
      addSurveyVisible: false,
      surveyRules: {
        temperature: [{ required: true, message: '请输入温度', trigger: 'blur' }],
        humidity: [{ required: true, message: '请输入湿度', trigger: 'blur' }],
        surveyDate: [{ required: true, message: '请选择日期', trigger: 'blur' }],
        surveyTime: [{ required: true, message: '请选择时间', trigger: 'blur' }]
      },
      seeSurveyVisible: false,
      seeSurveyForm: {
        temperature: '',
        humidity: '',
        surveyDate: '',
        surveyTime: ''
      },
      lookSurveyNoteVisible: false,
      searchTwoForm: {
        ambientDeviceId: '',
        startTime: '',
        endTime: ''
      },
      surveyTwoData: [],
      surveyDateCon: '',
      surveyTimeCon: '',
    }
  },
  created() {
    this.handleSearch()
    this.getsLocation()
  },
  methods: {
    handleSearch() {
      this.searchForm.monitorLocation = this.positionTwo
      ambientServer.findsAmbientSurvey(this.searchForm).then(res => {
        if (res.code === 200 && res.result) {
          this.surveyData = res.result.records || []
          this.total = res.result.total
        }
      })
    },

    // 监测位置列表(三级展开)
    getsLocation() {
      instrServer.roomList().then(res => {
        this.locationArray = res.result;
      });
    },

    // 转换数据结构
    normalizerTwo(node) {
      if (node.childList && !node.childList.length) {
        delete node.childList;
      }
      return {
        id: node.id,
        label: node.name,
        children: node.childList
      };
    },

    // 记录温湿度
    noteSurvey(row) {
      let date = new Date()
      let Y = date.getFullYear()
      let M = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : (date.getMonth() + 1)
      let D = date.getDate() < 10 ? ('0' + date.getDate()) : date.getDate()
      let hours = date.getHours()
      let minutes = date.getMinutes() < 10 ? ('0' + date.getMinutes()) : date.getMinutes()
      let seconds = date.getSeconds() < 10 ? ('0' + date.getSeconds()) : date.getSeconds()
      this.surveyDateCon = Y + '-' + M + '-' + D
      this.surveyTimeCon = hours + ':' + minutes + ':' + seconds
      this.surveyForm = {
        temperature: '',
        humidity: '',
        surveyDate: '',
        surveyTime: '',
        ambientDeviceId: ''
      };
      this.surveyForm.ambientDeviceId = row.id
      this.addSurveyVisible = true
    },

    // 保存记录的温湿度
    confirmSurvey() {
      this.surveyForm.surveyDate = this.surveyDateCon
      this.surveyForm.surveyTime = this.surveyTimeCon
      this.$refs.surveyForm.validate(valid => {
        if (valid) {
          ambientServer.recordTemperatureAndHumidity(this.surveyForm).then(res => {
            if (res.code === 200) {
              this.$notify({
                title: '成功',
                message: res.message,
                type: 'success'
              })
              this.addSurveyVisible = false;
              this.surveyForm = {
                temperature: '',
                humidity: '',
                surveyDate: '',
                surveyTime: '',
                ambientDeviceId: ''
              };
              this.handleSearch();
            } else {
              this.$notify({
                title: '失败',
                message: res.message,
                type: 'error'
              })
            }
          })
        }
      })
    },

    selectDate(val){
      this.surveyForm.surveyDate = val
    },
    selectTime(val){
      this.surveyForm.surveyTime = val
    },

    // 查看
    seeSurvey(row) {
      this.seeSurveyForm.temperature = row.temperature
      this.seeSurveyForm.humidity = row.humidity
      this.seeSurveyForm.surveyDate = row.surveyDate
      this.seeSurveyForm.surveyTime = row.surveyTime
      this.seeSurveyVisible = true
    },

    // 查看监测记录
    findAmbientSurveyNote(row) {
      this.surveyTwoData = []
      this.searchTwoForm.startTime = ''
      this.searchTwoForm.endTime = ''
      this.searchTwoForm.ambientDeviceId = row.id
      ambientServer.findAmbientsSurveyNote(this.searchTwoForm).then(res => {
        if (res.code === 200 && res.result) {
          this.surveyTwoData = res.result || []
        }
        this.lookSurveyNoteVisible = true
      })
    },

    // 筛选
    screenAmbientSurveyNote() {
      if (this.searchTwoForm.endTime !== '' && this.searchTwoForm.startTime !== ''){
        if (this.searchTwoForm.endTime < this.searchTwoForm.startTime) {
          this.$message.warning("结束时间不能早于开始时间!");
          return;
        }
      }
      ambientServer.findAmbientsSurveyNote(this.searchTwoForm).then(res => {
        this.surveyTwoData = []
        if (res.code === 200 && res.result) {
          this.surveyTwoData = res.result || []
        }
      })
    },

    // 下载监测记录
    exportsSurvey() {
      var url = '/api/ambient/exportSurvey?ambientDeviceId=' + this.searchTwoForm.ambientDeviceId + '&startTime=' + this.searchTwoForm.startTime + '&endTime=' + this.searchTwoForm.endTime
      window.open(url)
    },

  }
}
</script>

<style scoped lang="scss" type="text/scss">
.search_div {
  >div {
    &:nth-child(1){
      margin-right: 10px;
    }
    display: inline-block;
  }
}
.search_input {
  width: 200px;
  margin-bottom: 5px;
}
.search-right {
  text-align: right;
  padding-right: 25px;
}
.item {
  font-size: 16px;
}
::v-deep .el-card .el-card__header {
  padding: 5px 20px;
  background: #F3F2F2;
}
.apostrophe{
  text-overflow: ellipsis;
  overflow: hidden;
  white-space: nowrap;
}
.el-button{
  user-select: unset;
}
</style>
